package com.huangpuweb.task.controller;

import com.huangpuweb.task.Constants;
import com.huangpuweb.task.model.SysHistoryCrowdSituation;
import com.huangpuweb.task.model.SysHpScenicState;
import com.huangpuweb.task.model.SysRefinedCrowdSituation;
import com.huangpuweb.task.service.SyncSysDataService;
import com.huangpuweb.task.util.DateUtil;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.UUID;


/**
 * 每天23点59分00秒清洗当日的数据（表Sys_refined_situation)到历史表中（sys_history_situation)
 */
@Controller
public class SyncCleanDataJob {

    private static Logger logger = Logger.getLogger(SyncCleanDataJob.class);



    private SyncSysDataService syncSysDataService;
    @Autowired
    public void setSyncSysDataService(SyncSysDataService syncSysDataService) {
        this.syncSysDataService = syncSysDataService;
    }

    public void SyncCleanDataJobTask() throws Exception {

        LogFactory.getLog(this.getClass()).info("--------start-------SyncCleanDataJobTask----------" + new Date());

        //开始清洗计算当日各个景区数据
        String startTime="";
        String endTime="";
        //获取各个景区编码
        String spotcodelist = Constants.SPOTCODELIST;
        String[] spotCodeArr = spotcodelist.split(",");
        //获取当天0点
        String todayZero=DateUtil.getTodayZero();
//        if (null == startTime) {
//            todayZero= DateUtil.getTodayZero();
//        }else{
//            todayZero=startTime;
//        }
        //获取当天23点59分
        String today23= DateUtil.getToday23(23,59,0);
//        if (null == endTime) {
//            today23= DateUtil.getToday23(23,59,0);
//        }else {
//            today23=endTime;
//        }




        //获取景区编码、景区名称、景区总人流量、景区数据来源
        SysRefinedCrowdSituation refinedCrowdSituation=null;
        SysRefinedCrowdSituation _refinedCrowdSituation=null;
        SysHistoryCrowdSituation historyCrowdSituation =null;
        SysHpScenicState sysHpScenicState=null;


        for (int i = 0; i < spotCodeArr.length; i++) {


            refinedCrowdSituation=syncSysDataService.getDateNumberBySysRefinedCrowdSituation(spotCodeArr[i],todayZero,today23);

            //获取最新的实施人流量和今日总人流量
            _refinedCrowdSituation=syncSysDataService.getDateRealNumberBySysRefinedCrowdSituation(spotCodeArr[i],todayZero,today23);

            //获取景区人流量峰值、峰值时间

            if (refinedCrowdSituation!=null && _refinedCrowdSituation!=null ){
                //获取凌晨0点创建的那条数据
                //dao().fetch(Sys_history_crowdSituation.class, Cnd.where("spotCode", "=", spotCodeArr[i]).and("recordTIme", "=", todayZero))
                historyCrowdSituation = syncSysDataService.getFirstSysHistoryCrowdSituation(spotCodeArr[i],todayZero);
                boolean flag=false;
                if (null == historyCrowdSituation) {
                    historyCrowdSituation = new SysHistoryCrowdSituation();
                    flag=true;
                }

                //设置景区编码、景区名称、景区数据来源、景区今日总人流量
                historyCrowdSituation.setDataSources(refinedCrowdSituation.getDataSources());
                historyCrowdSituation.setSpotCode(refinedCrowdSituation.getSpotCode());
                String spotName = refinedCrowdSituation.getSpotName();
                historyCrowdSituation.setSpotName(spotName);
                historyCrowdSituation.setTotalNumber(refinedCrowdSituation.getTotalNumber());

                //设置景区今日峰值、峰值时间
                historyCrowdSituation.setMaxNumber(_refinedCrowdSituation.getRealtimeNumber());
                historyCrowdSituation.setMaxTime(_refinedCrowdSituation.getCreateTime());
                if (!flag) {
                    //更新
                    syncSysDataService.updateHistoryCrowdSituation(historyCrowdSituation);
                }else {
                    //新插入
                    historyCrowdSituation.setRecordTime(todayZero);
                    historyCrowdSituation.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    syncSysDataService.insertHistoryCrowdSituation(historyCrowdSituation);
                }
            }


            //获取第二天的0点0分0秒
            String tomorrowZero=  DateUtil.getTomorrowZero();
            SysHistoryCrowdSituation tomorrowHistory = syncSysDataService.getFirstSysHistoryCrowdSituation(spotCodeArr[i],tomorrowZero);


            if (null == tomorrowHistory) {
                historyCrowdSituation = new SysHistoryCrowdSituation();
                historyCrowdSituation.setWarningNumber(0);
                //时间设置为第二天0点0分0秒
                historyCrowdSituation.setRecordTime(tomorrowZero);
                historyCrowdSituation.setSpotName(refinedCrowdSituation.getSpotName());
                historyCrowdSituation.setTotalNumber(0);
                historyCrowdSituation.setDataSources(refinedCrowdSituation.getDataSources());
                historyCrowdSituation.setMaxNumber(0);
                historyCrowdSituation.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                historyCrowdSituation.setSpotCode(spotCodeArr[i]);
                syncSysDataService.insertHistoryCrowdSituation(historyCrowdSituation);
            }

        }

        LogFactory.getLog(this.getClass()).info("--------end-------SyncCleanDataJobTask----------" + new Date());

    }


}
